package cn.tjie.pet.service.impl;

import cn.tjie.basic.service.IBaiduAiAuditService;
import cn.tjie.basic.util.AjaxResult;
import cn.tjie.pet.domain.Pet;
import cn.tjie.pet.domain.PetDetail;
import cn.tjie.pet.domain.PetOnlineAuditLog;
import cn.tjie.pet.mapper.PetDetailMapper;
import cn.tjie.pet.mapper.PetMapper;
import cn.tjie.pet.mapper.PetOnlineAuditLogMapper;
import cn.tjie.pet.service.IPetService;
import cn.tjie.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.save(petDetail);
        }
    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delDetailByPetId(id);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            if(boo1 && boo2){
                //改状态
                petMapper.onsale(id,new Date());
                //添加审核日志
                log.setState(1);//审核成功
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);

            }else{
                //添加审核日志
                log.setState(0);//审核失败
                log.setNote("审核失败");
                petOnlineAuditLogMapper.save(log);
                return  new AjaxResult(false,"上架失败");
            }
        }
        //1.上架审核【内容审核，图像审核】 - 每一个宠物
        //2.审核失败：状态不改 ，要添加审核日志 - 审核失败为0
        //3.审核成功：状态改为1 ，要添加审核日志 - 审核成功为1
        return new AjaxResult();
    }

    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        //改状态 //方式1：先查再改   //方式2：直接改需要写sql
        //Mybatis多参数操作：1.Map 2.对象 3.索引下标#{0} 4.注解
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
